{% extends "templates/main_template.html.j2" %}

{% import "macros/select_options.html.j2" as SELECT_OPTIONS %}
{% import "macros/switch.html.j2" as SWITCH %}
{% import "macros/modal_confirmation.html.j2" as CONFIRMATION %}
{% import "macros/markdown_textarea.html.j2" as MARKDOWN_TEXTAREA %}
{% import "macros/tab_switcher.html.j2" as TAB_SWITCHER %}


{% block title -%}
  {%- if target_issue_kind -%}
    {{ target_issue_kind.caption|e }} · Тип запроса · Farado
  {%- else -%}
    Новый тип запроса · Farado
  {%- endif -%}
{% endblock %}

{% set active_menu = 'issue_kinds' %}
{% set tab_menu = [
  ['Свойства', 'properties'],
  ['Поля', 'fields'],
] %}


{#----------------------------------------------------------------------------#}
{% block styles %}
  <link href="/static/css/page-toolbar.css" rel="stylesheet" type="text/css" />
  {{ SELECT_OPTIONS.STYLES() }}
{% endblock %}


{#----------------------------------------------------------------------------#}
{% block breadcrumb %}
  <ol class="breadcrumb">
    <li class="breadcrumb-item active" aria-current="page">
      {% if target_issue_kind %}
        {{ target_issue_kind.caption|e }}
      {% else %}
        Новый тип запроса
      {% endif %}
    </li>
  </ol>
{% endblock %}


{#----------------------------------------------------------------------------#}
{# Таб 1 — Свойства #}
{% block tab_content_1 %}
  <form class="row g-3 mt-3" action="/issue_kinds/issue_kind/{{ target_issue_kind.id }}" method="post">

    {# -- Название -- #}
      <div class="col-4">
        <label for="inputCaption" class="form-label">Название</label>
        <input
            type="text"
            name="target_issue_kind_caption"
            class="form-control"
            id="inputCaption"
            value="{{ target_issue_kind.caption|e }}"
            required>
      </div>
    {# -- /Название -- #}

    {# -- Рабочий процесс -- #}
      <div class="col-4">
        <label for="textareaDescription" class="form-label">Рабочий процесс</label>
        {{ SELECT_OPTIONS.INPUT_SINGLE(
            data_source = 'workflows',
            name = 'target_issue_kind_workflow_id',
            project_manager = project_manager,
            selected_id = target_issue_kind.workflow_id )
        }}
      </div>
    {# -- /Рабочий процесс -- #}

    {# -- Состояние по умолчанию -- #}
      <div class="col-4">
        <label for="textareaDescription" class="form-label">Состояние по умолчанию</label>
        {{ SELECT_OPTIONS.INPUT_SINGLE(
              data_source = 'workflows_states',
              name = 'target_issue_kind_default_state_id',
              project_manager = project_manager,
              selected_id = target_issue_kind.default_state_id )
        }}
      </div>
    {# -- /Состояние по умолчанию -- #}

    {# -- Контент запроса по умолчанию -- #}
      <div class="col-12">
        <label for="target_issue_kind_default_content" class="form-label">
          Контент запроса по умолчанию
        </label>
        {% if target_issue_kind %}
          {% set default_content_value = target_issue_kind.default_content_value()%}
        {% else %}
          {% set default_content_value = ''%}
        {% endif %}
        {{ MARKDOWN_TEXTAREA.INPUT(
              tab_id = 'target_issue_kind_default_content_tab',
              result_id = 'target_issue_kind_default_content_result',
              field_name = 'target_issue_kind_default_content',
              field_value = default_content_value)
        }}
        <script>
          document_ready_functions.push(function() {
            {{ TAB_SWITCHER.DOCUMENT_READY_SCRIPT(
                'target_issue_kind_default_content_tab')
            }}
            markdownToHtml(
              'target_issue_kind_default_content',
              'target_issue_kind_default_content_result'
            );
          });
        </script>
      </div>
    {# -- /Контент запроса по умолчанию -- #}

    <hr/>

    <div class="col-12">
      <div class="clearfix">
        {% if target_issue_kind %}
          {# -- Кнопка: удалить -- #}
            {{ CONFIRMATION.BUTTON(
                caption = '<i class="bi bi-trash"></i> Удалить',
                title = 'Удалить данный тип запроса',
                class = 'btn btn-danger ml-3 float-start',
                target = 'remove_issue_kind_confirmation_'
                    + target_issue_kind.id|string)
            }}
            {{ CONFIRMATION.MODAL_WINDOW(
                title = 'Удаление типа запроса',
                text = 'Вы уверены, что хотите навсегда удалить тип запроса «'
                    + target_issue_kind.caption|e
                    + '»?',
                action_button_href = '/issue_kinds/remove_issue_kind/'
                    + target_issue_kind.id|string,
                action_button_text = 'Да',
                window_id = 'remove_issue_kind_confirmation_'
                    + target_issue_kind.id|string)
            }}
          {# -- /Кнопка: удалить -- #}
        {% endif %}

        {# -- Кнопка: сохранить -- #}
          <button
              type="submit"
              id="saveButton"
              class="btn btn-success float-end">
            <i class="bi bi-save"></i> Сохранить
          </button>
        {# -- /Кнопка: сохранить -- #}
      </div>
    </div>

  </form>
{% endblock tab_content_1 %}


{#----------------------------------------------------------------------------#}
{# Таб 2 — Поля #}
{% block tab_content_2 %}
  {% if target_issue_kind %}
    <div class="col-12">
      <div class="page-toolbar d-flex justify-content-between align-items-center container-button">
        {# FIXME : If you add a new field kind, all issue_kind changes on this page are discarded. #}
        <a  href="/issue_kinds/add_field_kind?target_issue_kind_id={{ target_issue_kind.id }}"
            class="btn btn-outline-primary btn-sm"
            type="submit">
          <i class="bi bi-plus mr-1"></i> Создать поле
        </a>
      </div>

      <table class="table mt-4">
        <thead class="table-dark">
          <tr>
            <th scope="col" style="width: 1%">Id</th>
            <th scope="col">Название</th>
            <th scope="col">Описание</th>
            <th scope="col">Тип данных</th>
            <th scope="col">Значения</th>
            <th scope="col">Отображать на доске</th>
            <th scope="col" style="width: 1%"></th>
          </tr>
        </thead>
        <tbody>
          {% for field_kind in project_manager.field_kinds_by_issue_kind_id(target_issue_kind.id) %}
            <tr id="field_kind_row_{{ field_kind.id }}">
              <form id="field_kind_form_{{ field_kind.id }}" action="/issue_kinds/save_field_kind" method="post">
                <th scope="row">{{ field_kind.id }}</th>
                <td>
                  <!-- FieldKind.caption -->
                  <input
                    type="text"
                    name="target_field_kind_caption"
                    class="form-control form-select-sm"
                    value="{{ field_kind.caption|e }}"
                    required>
                </td>
                <td>
                  <!-- FieldKind.description -->
                  <textarea
                    name="target_field_kind_description"
                    class="form-control form-select-sm"
                    rows="1">{{ field_kind.description|e }}</textarea>
                </td>
                <td>
                  {# -- FieldKind.value_type -- #}
                    {{ SELECT_OPTIONS.INPUT_SINGLE(
                        data_source = 'value_types',
                        name = 'target_field_kind_value_type',
                        project_manager = project_manager,
                        selected_id = field_kind.value_type,
                        need_zero_value = False)
                    }}
                  {# -- /FieldKind.value_type -- #}
                </td>
                <td>
                  <textarea
                      name="target_field_kind_values"
                      class="form-control form-select-sm"
                      rows="1"
                      title="Перечень значений можно задать через запятую или переход на новую строку.">
                    {{- field_kind.get_values() | join('\n') -}}
                  </textarea>
                </td>
                <td>
                  <!-- FieldKind.is_system -->
                  {# FIXME: флаг is_system пока отвечает за то, отображается ли на доске данное поле #}
                  {{ SWITCH.INPUT(
                        id = 'target_field_kind_is_system_' + field_kind.id|string,
                        name = 'target_field_kind_is_system',
                        is_checked = field_kind.is_system )
                  }}
                </td>
                <td>
                  <div style="white-space: nowrap">
                    <input type="hidden" name="target_field_kind_id" value="{{ field_kind.id }}">
                    <input type="hidden" name="target_issue_kind_id" value="{{ target_issue_kind.id }}">
                    <!-- FieldKind.save() -->
                    <a href="#" onclick="$('#field_kind_form_{{ field_kind.id }}').submit();"><span class="badge bg-primary">v</span></a>
                    <!-- FieldKind.remove() -->
                    <a href="/issue_kinds/remove_field_kind?target_issue_kind_id={{ target_issue_kind.id }}&target_field_kind_id={{ field_kind.id }}" title="Remove field kind"><span class="badge bg-danger">x</span></a>
                  </div>
                </td>
              </form>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  {% endif %}
{% endblock tab_content_2 %}


{#----------------------------------------------------------------------------#}
{% block scripts %}
  {{ MARKDOWN_TEXTAREA.SCRIPTS() }}
  {{ SELECT_OPTIONS.SCRIPTS() }}
{% endblock %}
